//
//  PlayerDataBase.m
//  WXMusic
//
//  Created by 漫步人生路 on 15/9/9.
//  Copyright (c) 2015年 漫步人生路. All rights reserved.
//

#import "PlayerDataBase.h"

@implementation PlayerDataBase
static PlayerDataBase *playerDataBase;
+ (PlayerDataBase *)shareDataBase
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        playerDataBase = [[PlayerDataBase alloc]init];
        
        
        NSLog(@"%@",[NSThread currentThread]);
    });
    return playerDataBase;
}
//创建fmdb
- (FMDatabase *)dataBase
{
    if (!_dataBase) {
        _dataBase = [[FMDatabase alloc]init];
    }
    return _dataBase;
}

//指定数据库路径
- (NSString *)sqlitePaths:(NSString *)sqlitePaths
{
    NSString *str = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:sqlitePaths];
    NSLog(@"%@",str);
    return str;
}
//打开数据库
- (void)openSqliteWithPaths:(NSString *)paths
{
    self.dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:paths]];
}
//关闭数据
- (void)closeSqlite
{
    [self.dataBase close];
}
//创建数据库表
- (void)createTables
{
    if (!_dataBase) {
        [self openSqliteWithPaths:@"SongList.db"];
    }
    if ([_dataBase open]) {
        BOOL executeUpdate = [_dataBase executeUpdate:@"CREATE TABLE JokesTable(JokeName TEXT  PRIMARY KEY )"];
        if (executeUpdate) {
            NSLog(@"建表1成功");
        }else
        {
            NSLog(@"建表1失败");
        }
    }


   if ([_dataBase open]) {
      BOOL executeUpdate = [_dataBase executeUpdate:@"CREATE TABLE PicsTable(PicsName TEXT PRIMARY KEY,PicUrl TEXT )"];
    if (executeUpdate) {
        NSLog(@"建表2成功");
    }else
    {
        NSLog(@"建表2失败");
    }
  }
    if ([_dataBase open]) {
    BOOL executeUpdate = [_dataBase executeUpdate:@"CREATE TABLE FortuneTable(FortunePath TEXT PRIMARY KEY)"];
    if (executeUpdate) {
        NSLog(@"建表3成功");
    }else
    {
        NSLog(@"建表3失败");
    }
    }
    if ([_dataBase open]) {
        BOOL executeUpdate = [_dataBase executeUpdate:@"CREATE TABLE cityNameTable(cityName TEXT PRIMARY KEY)"];
        if (executeUpdate) {
            NSLog(@"建表4成功");
        }else
        {
            NSLog(@"建表4失败");
        }
    }
    if ([_dataBase open]) {
        BOOL executeUpdate = [_dataBase executeUpdate:@"CREATE TABLE cityNameTable(cityName TEXT PRIMARY KEY)"];
        if (executeUpdate) {
            NSLog(@"建表4成功");
        }else
        {
            NSLog(@"建表4失败");
        }
    }



}

//插入表1数据
- (void)insertJokeModelToJokeTable:(LatestJokeModel *)model
{
    if (!model) {
        NSLog(@"Jokemodel为空");
        return;
    }
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"insert into JokesTable(JokeName) values(?)"];
        BOOL insertModel = [_dataBase executeUpdate:sql,model.content];
        
        if (insertModel) {
            NSLog(@"插入成功");
        }else
        {
            NSLog(@"插入失败");
        }
    }
}
//插入表2数据
- (void)insertPicModelToPicTable:(FunPicModel *)model
{
    if (!model) {
        NSLog(@"PicModel为空");
        return;
    }
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"insert into PicsTable(PicsName,PicUrl) values(?,?)"];
        BOOL insertModel = [_dataBase executeUpdate:sql,model.content,model.url];
        
        if (insertModel) {
            NSLog(@"插入成功");
        }else
        {
            NSLog(@"插入失败");
        }
    }
}
//插入表3数据
- (void)insertFortunePaperModel:(FortunePaperModel *)model{
    if (!model) {
        NSLog(@"Jokemodel为空");
        return;
    }
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"insert into FortuneTable(FortunePath) values(?)"];
        BOOL insertModel = [_dataBase executeUpdate:sql,model.path];
        
        if (insertModel) {
            NSLog(@"插入成功");
        }else
        {
            NSLog(@"插入失败");
        }
    }
  
}
//插入表4
- (void)insertcityNameTable:(NSString *)city
{
    if (!city) {
        NSLog(@"cityName为空");
        return;
    }
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"insert into cityNameTable(cityName) values(?)"];
        BOOL insertCity = [_dataBase executeUpdate:sql,city];
        
        if (insertCity) {
            NSLog(@"插入成功");
        }else
        {
            NSLog(@"插入失败");
        }
    }
}


//返回表1数据
- (NSMutableArray *)selegetAllFromJokeTable
{
    NSMutableArray *model = [[NSMutableArray alloc]init];
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = @"select * from JokesTable";
        FMResultSet *rs = [_dataBase executeQuery:sql];
        while ([rs next]) {
            LatestJokeModel *jokemodel = [[LatestJokeModel alloc]init];
           
          jokemodel.content = [rs stringForColumn:@"JokeName"];
          
           
            [model addObject:jokemodel];
        }
    }
    return model;
}

//返回表2数据
- (NSMutableArray *)selegetAllFromPicTable
{
    NSMutableArray *model = [[NSMutableArray alloc]init];
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = @"select * from PicsTable";
        FMResultSet *rs = [_dataBase executeQuery:sql];
        while ([rs next]) {
            FunPicModel *picmodel = [[FunPicModel alloc]init];
            
            picmodel.content = [rs stringForColumn:@"PicsName"];
          picmodel.url = [rs stringForColumn:@"PicUrl"];
            
            [model addObject:picmodel];
        }
    }
    return model;
}
//获取表3数据
- (NSMutableArray *)selegetAllFormFortunePapers{
    _model = [[NSMutableArray alloc]init];
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = @"select * from FortuneTable";
        FMResultSet *rs = [_dataBase executeQuery:sql];
        while ([rs next]) {
            FortunePaperModel *Papermodel = [[FortunePaperModel alloc]init];
            
            Papermodel.path = [rs stringForColumn:@"FortunePath"];
            
            
            [_model addObject:Papermodel];
        }
    }
    return _model;
}
//返回表4
- (NSMutableArray *)selegetAllFromCityNameTable
{
    NSMutableArray *cityName = [[NSMutableArray alloc]init];
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = @"select * from cityNameTable";
        FMResultSet *rs = [_dataBase executeQuery:sql];
        while ([rs next]) {
            NSString *string = [NSString string];
            
            string = [rs stringForColumn:@"cityName"];
            
            
            [cityName addObject:string];
        }
    }
    return cityName;
}

//删除表1数据
- (void)deleteJokesWithJokeName:(NSString *)content
{
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from JokesTable where JokeName = ?"];
        BOOL b = [_dataBase executeUpdate:sql,[NSString stringWithFormat:@"%@",content]];
        if (b) {
            NSLog(@"删除成功");
        }else{
            NSLog(@"删除失败");
        }

    }
}
//删除表2数据
- (void)deletePicsWithPicName:(NSString *)content
{
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from PicsTable where PicsName = ?"];
        BOOL b = [_dataBase executeUpdate:sql,[NSString stringWithFormat:@"%@",content]];
        if (b) {
            NSLog(@"删除成功");
        }else{
            NSLog(@"删除失败");
        }
        
    }
}
//删除表3图片
- (void)deleteFortunePaperModelPath:(NSString *)path{
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from FortuneTable where FortunePath = ?"];
        BOOL b = [_dataBase executeUpdate:sql,[NSString stringWithFormat:@"%@",path]];
        if (b) {
            NSLog(@"删除成功");
        }else{
            NSLog(@"删除失败");
        }
        
    }
   
}
//删除表1数据
- (void)deleteCityWithCityName:(NSString *)content
{
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from cityNameTable where cityName = ?"];
        BOOL b = [_dataBase executeUpdate:sql,[NSString stringWithFormat:@"%@",content]];
        if (b) {
            NSLog(@"删除成功");
        }else{
            NSLog(@"删除失败");
        }
        
    }
}
@end
